package com.jk.service;


import com.jk.entity.Payment;
import com.jk.entity.TransferRecord;
import com.jk.entity.YuEBao;
import com.jk.mapper.YuebaoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by 动物的 on 2017/12/21.
 */
@Service("yuebaoService")
public class YuebaoServiceImpl implements YuebaoService{

      @Autowired
      private YuebaoMapper yuebaoMapper;

    /**
     * 查询余额宝
     * @param userId
     * @return
     */
    public YuEBao selectYuebao(String userId) {
        return yuebaoMapper.selectYuebao(userId);
    }

    /**
     * 转入（将账户余额转入余额宝余额）
     * @param userId
     * @param shiftTo
     * @return
     */
    public boolean updateYuebao(String userId, double shiftTo) {
        Payment payMent = yuebaoMapper.selectZhye(userId);
          if(payMent.getAccountBalance()>=shiftTo){
              yuebaoMapper.updYuebao(userId,shiftTo);
              yuebaoMapper.udaZhyue(userId,shiftTo);


              TransferRecord tr = new TransferRecord();
              tr.setId(UUID.randomUUID().toString().replace("-",""));
              tr.setRecordName("余额宝-单次转入");
              tr.setDealMoney(shiftTo);
              tr.setDealStatus("交易成功");
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
              String ss = sdf.format(new Date());
              tr.setDealDate(ss);
              tr.setUserId(userId);
              yuebaoMapper.addRecord(tr);

              return true;
          }else{
              return false;
          }

    }

    /**
     * 转出（将余额宝余额转出账户余额）
     * @param userId
     * @param rollOut
     * @return
     */
    public boolean updateZhuanchu(String userId, double rollOut) {
        YuEBao yuebao = yuebaoMapper.selectYuebao(userId);
        if(yuebao.getExistingYue()>=rollOut){
            yuebaoMapper.updYuebaoZhuanchu(userId,rollOut);
            yuebaoMapper.udaZhyueZhuanchu(userId,rollOut);


            TransferRecord tr = new TransferRecord();
            tr.setId(UUID.randomUUID().toString().replace("-",""));
            tr.setRecordName("余额宝-转出到余额");
            tr.setDealMoney(rollOut);
            tr.setDealStatus("交易成功");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String ss = sdf.format(new Date());
            tr.setDealDate(ss);
            tr.setUserId(userId);
            yuebaoMapper.addRecord(tr);
            return true;
        }else{
            return false;
        }

    }

    public List<TransferRecord> getTransferRecord(String userId) {
        return yuebaoMapper.getTransferRecord(userId);
    }


}
